package com.keyancompany.zhongbaopingtai.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.RewardAdapter;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.listener.IAsyncHttpResponseHandler;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.FindModel;
import com.keyancompany.zhongbaopingtai.model.ProjectTalkComment;
import com.keyancompany.zhongbaopingtai.weight.IListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.OnClick;

/**
 * 悬赏讨论
 */
public class RewardActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {


    @BindString(R.string.reward_title)
    String rewardTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.listView)
    IListView listView;
    @Bind(R.id.swipe_layout)
    SwipeRefreshLayout swipeLayout;
    @Bind(R.id.to_reward_conent)
    EditText toRewardConent;
    private List<ProjectTalkComment> lists;
    private RewardAdapter mAdapter;
    private int total = 1, currentPage = 1;
    private String projectId;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_reward;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar,rewardTitle);
        initSwipeLayout(swipeLayout);
    }

    @Override
    protected void setUpViews() {
        lists = (List<ProjectTalkComment>) getIntent().getExtras().getSerializable(Constants.ITEM);
        projectId = getIntent().getExtras().getString("projectId");
        total = getIntent().getExtras().getInt(Constants.TOTAL);
        mAdapter = new RewardAdapter(lists,mContext);

        listView.setAdapter(mAdapter);

        swipeLayout.setOnRefreshListener(this);

        listView.setStateListener(new IListView.SwipeLayoutState() {
            @Override
            public boolean isRefresh() {
                return swipeLayout.isRefreshing();
            }

            @Override
            public boolean isLastPage() {
                return total == currentPage;
            }

            @Override
            public void requestNextPage() {
                requestTask(++currentPage);
            }
        });
    }

    @OnClick(R.id.to_reward_send)
    public void toSend(View view) {
//        TeamAPI.saveTeamTalk();
        String content = toRewardConent.getText().toString();
        if (TextUtils.isEmpty(content)) {
            return;
        }
        UserAPI.submitComment(projectId, content, null, this, getLoading(R.string.watting_hint));

    }

    private void requestTask(int currentPage) {

        UserAPI.searchComment(projectId, "" + currentPage, this, null);
    }


    @Override
    public void success(int type, String json) {
        swipeLayout.setRefreshing(false);
        BaseModel base = validateJson(json);
        if (base == null) {
            return;
        }
        if (type == UserAPI.COMMENT_SUBMIT) {
            if (base.isSuccess()) {
                toRewardConent.setText("");
                currentPage = 1;
                swipeLayout.post(new Thread(new Runnable() {

                    @Override
                    public void run() {
                        swipeLayout.setRefreshing(true);
                        onRefresh();
                    }
                }));
            }

        } else if (type == UserAPI.SEARCH_COMMENT) {

            if (currentPage == 1) {
                mAdapter.clear();
            } else {

            }
            JSONObject jsonObject = JSONObject.parseObject(base.data);
            JSONObject pager = jsonObject.getJSONObject("pager");
            currentPage = pager.getInteger(Constants.CURRENT);
            total = pager.getInteger(Constants.TOTAL);

            List<ProjectTalkComment> lists = JSONArray.parseArray(pager.getString("list"), ProjectTalkComment.class);
            if (mAdapter == null) return;
            listView.loadMoreFinish();
            if (total == currentPage) {
                listView.removeFooterView();
            }
            mAdapter.addAllAfter(lists);
        }

    }

    @Override
    public void onRefresh() {
        currentPage = 1;

        requestTask(currentPage);
    }
}
